package com.entities.report.detail;



import com.core.protocols.ICustomDatasource;
import com.core.protocols.ICustomDatasourceDelegate;
import com.core.protocols.IDescriptionViewDelegate;
import com.core.protocols.IDetailTableViewDelegate;
import com.core.protocols.IServiceLayerDelegate;
import com.core.ui.controller.base.HibernateDetailController;
import com.core.utils.LocalizableString;
import com.entities.report.servicelayer.ReportListLayer;
import com.model.report.Report;


public class ReportDetailController
        extends HibernateDetailController
        implements IDetailTableViewDelegate, IDescriptionViewDelegate, IServiceLayerDelegate, ICustomDatasource {



	private Report          reportItem;
	private ReportListLayer reportListLayer;
	private String          reportId;



	@Override
	public void setCustomDatasourceDelegate ( ICustomDatasourceDelegate delegate ) {

		// TODO Auto-generated method stub

	}



	@Override
	public void reloadData () {

		Integer result = null;
		try {
			result = Integer.parseInt(this.reportId);
		} catch ( NumberFormatException e ) {
			result = null;
		}


		reportItem = (Report) reportListLayer.objectByUNID(result, null, this);
	}



	public String getReportId () {

		return reportId;
	}



	public void setReportId ( String reportId ) {

		this.reportId = reportId;
	}



	public ReportDetailController() {

		reportListLayer = new ReportListLayer();


	}



	@Override
	public String descriptionData () {

		// TODO Auto-generated method stub
		return null;
	}



	@Override
	public int detailTableViewItemsCount ( String userInfo ) {

		int result = 0;
		String userInfoUC = userInfo.toUpperCase();
		if ( userInfoUC.equals("BASE") ) {
			result = 3;
		}
		return result;
	}



	@Override
	public String detailTableViewItemAtIndex ( String userInfo,
	                                           int index ) {

		String result = null;

		if ( userInfo.toUpperCase().equals("BASE") ) {
			result = this.reportBaseItemAtIndex(index);
		}
		return result;
	}



	private String reportBaseItemAtIndex ( int index ) {

		String result = null;

		String style = null;
		String propertyValue = null;
		String data = null;
		String captionDescription = LocalizableString.sharedInstance().localizable("entity.Report.field.description");
		String captionName = LocalizableString.sharedInstance().localizable("entity.Report.field.name");
		String captionSQLScript = LocalizableString.sharedInstance().localizable("entity.Report.field.sqlscript");

		boolean readOnly = this.isReadOnly();

		switch ( index ) {
			case 0: {
				style = "-single";
				if ( reportItem != null ) {
					propertyValue = reportItem.getName();
				}
				data = this.inputControl("name", captionName, propertyValue, readOnly);
				break;
			}
			case 1: {
				style = "-single";
				if ( reportItem != null ) {
					propertyValue = reportItem.getDescription();
				}
				data = this.textAreaControl("description", captionDescription, propertyValue, readOnly);
				break;
			}
			case 2: {
				style = "-single";
				if ( reportItem != null ) {
					propertyValue = reportItem.getSqlScript();
				}
				data = this.textAreaControl("sqlScript", captionSQLScript, propertyValue, readOnly);
				break;
			}
			default: {
				break;
			}
		}
		result = String.format("<li class=\"property-item%s\"><div class=\"wrap\">%s</li>", style, data);
		return result;
	}
}
